package blog.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;

import blog.entity.Comment;
import blog.service.CommentService;

@RequestMapping("comment")
@Controller
public class CommentController {
	@Autowired
	private CommentService commentService;
	@RequestMapping("")
	public String commentList(ModelMap map,@RequestParam(required=false,defaultValue="1")Integer pageIndex,
	@RequestParam(required=false,defaultValue="10")Integer pageSize) {
		PageInfo<Comment> findAllPageComment = commentService.findAllPageComment(pageIndex,pageSize);
		map.put("pageUrlPrefix", "comment?pageIndex");
		map.put("pageInfo", findAllPageComment);
		return "comment/comment_list";
	}
	/**
	 * 去回复评论界面
	 * @param commentId
	 * @return
	 */
	@RequestMapping("/reply/{commentId}")
	public String goCommentReply(@PathVariable("commentId")Integer commentId,ModelMap map) {
		Comment comment = commentService.getCommentById(commentId);
		map.put("comment", comment);
		return "comment/comment_reply";
	}
	/**
	 * 添加评论
	 * @param map
	 * @param comment
	 * @return
	 */
	@RequestMapping("/replySubmit")
	public String commentReply(ModelMap map,Comment comment) {
		comment.setCommentCreateTime(new Date());
		commentService.addComment(comment);
		return "forward:/comment";
	}
	@RequestMapping("/delete/{commentId}")
	public String deleteComment(@PathVariable("commentId")Integer commentId) {
		commentService.deleteComment(commentId);
		return "forward:/comment";
	}
}
